#include "bg_star.h"
#include "../core/scene.h"
bg_star* bg_star::createBgStar(node *parent, int num, float scale_far, float scale_mid, float scale_near)
{
    bg_star *bg = new bg_star();
    bg->init();
    bg->num_ = num;
    bg->scale_far = scale_far;
    bg->scale_mid = scale_mid;
    bg->scale_near = scale_near;
    bg->star_far.reserve(num);
    bg->star_mid.reserve(num);
    bg->star_near.reserve(num);
    auto extra = game::instance().getCurrentScene()->getWorldSize() - game::instance().getScreenSize();
    for(int i = 0; i < num; i++)
    {
        bg->star_far.push_back(game::instance().randomVec2(glm::vec2(0),
        game::instance().getScreenSize() + extra * scale_far));
        bg->star_mid.push_back(game::instance().randomVec2(glm::vec2(0),
        game::instance().getScreenSize() + extra * scale_mid));
        bg->star_near.push_back(game::instance().randomVec2(glm::vec2(0),
        game::instance().getScreenSize() + extra * scale_near));
    }
    if(parent)
        parent->addChild(bg);
    return bg;
}

void bg_star::update(float dt)
{
    time_ += dt;
    color_far = {0.5f + 0.5f * sinf(time_* 0.9f), 0.5f * sinf(time_* 0.8f), 0.5f * sinf(time_* 0.7f), 1.0f};
    color_mid = {0.5f + 0.5f * sinf(time_* 0.7f), 0.5f * sinf(time_* 0.6f), 0.5f * sinf(time_* 0.5f), 1.0f};
    color_near = {0.5f + 0.5f * sinf(time_* 0.65f), 0.5f * sinf(time_* 0.5f), 0.5f * sinf(time_* 0.35f), 1.0f};
}

void bg_star::render()
{
    game_.drawPoint(star_far, - game_.getCurrentScene()->getCameraPosition() * scale_far, color_far);
    game_.drawPoint(star_mid, - game_.getCurrentScene()->getCameraPosition() * scale_mid, color_mid);
    game_.drawPoint(star_near, - game_.getCurrentScene()->getCameraPosition() * scale_near, color_near);
}
